﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Admin.Business.DailyPrayers
{
    public static class CreateLocation
    {
        public static Adams.Admin.DataTransfer.DailyPrayers.CreateLocation.PageLoad PageLoad(int id)
        {
            return DataAccess.DailyPrayers.CreateLocation.PageLoad(id);
        }

        public static DataTransfer.FormOutput SaveLocation(DataTransfer.DailyPrayers.CreateLocation.FormInput input)
        {

            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                // Is Master
                int currentmasterid = DataAccess.DailyPrayers.CreateLocation.GetCurrentMaster();
                if (input.IsMaster)
                {
                    if (currentmasterid > 0)
                    {
                        if (currentmasterid != input.ID.ToInt())
                        {
                            output.InvalidFields.Add(new DataTransfer.InvalidField()
                            {
                                Name = "IsMaster",
                                Code = 7
                            });
                        }
                    }
                }

                // InheritMaster
                if (input.InheritMaster)
                {
                    if (input.IsMaster)
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "InheritMaster",
                            Code = 8
                        });
                    }
                }

                if (output.IsValid)
                {
                    DataAccess.DailyPrayers.CreateLocation.Save(input);
                    output.IsSaved = true;
                }
            }

            return output;
        }
    }
}
